﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.Web.Admin.Modules.ShareCode;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.Admin.ShareCode
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Class : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            var type = Request.Form["t"];
            switch (type)
            {
                #region
                case "1":
                    GetShareCodeClassList();
                    break;
                case "2":
                    Add();
                    break;
                case "3":
                    Update();
                    break;
                case "4":
                    Delete();
                    break;
                default: break;
                #endregion
            }
        }

        public void GetShareCodeClassList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                UserControlPreviewManager<ShareCodeClassList> preivewManager = new UserControlPreviewManager<ShareCodeClassList>();
                ShareCodeClassList shareCodeClassList = preivewManager.LoadUserControl("~/Modules/ShareCode/ShareCodeClassList.ascx");
                result.message = preivewManager.RenderHtml(shareCodeClassList);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        public void Add()
        {
            #region
            var className = Request.Form["className"];
            AjaxActionResult result = new AjaxActionResult();
            if (!string.IsNullOrEmpty(className))
            {
                ShareCodeClassModel entity = new ShareCodeClassModel();
                entity.Name = className.Trim();

                result.success = ShareCodeManager.Add(entity);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        public void Update()
        {
            #region
            var className = Request.Form["className"];
            var id = 0;
            AjaxActionResult result = new AjaxActionResult();
            if (!string.IsNullOrEmpty(className) && int.TryParse(Request.Form["id"], out id))
            {
                ShareCodeClassModel entity = new ShareCodeClassModel();
                entity.Name = className.Trim();
                entity.ClassID = id;

                result.success = ShareCodeManager.Add(entity);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        public void Delete()
        {
            #region
            var id = 0;
            AjaxActionResult result = new AjaxActionResult();
            if (int.TryParse(Request.Form["id"], out id))
            {
                result.success = ShareCodeManager.DeleteShareCodeClass(id);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
